package com.km.core.utils;

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.km.core.dto.Dto;

/**
 * <p>参数验证工具</p>
 * Created by zhezhiyong@163.com on 2017/4/26.
 */
@SuppressWarnings("unused")
public class ParamsValidate {

    public static void validateEmpty(Dto dto, String... keys) {
        try {
            Preconditions.checkNotNull(keys);
            for (String key : keys) {
                if (Strings.isNullOrEmpty(dto.getAsString(key)))
                    throw new IllegalArgumentException(String.format("参数%s不能为空", key));
            }
        } catch (NullPointerException e) {
            throw new IllegalArgumentException(dto.toJson(), e);
        }
    }

    public static void validateLength(Dto dto, int min, int max, String... keys) {
        for (String key : keys) {
            validateEmpty(dto, key);
            int length = dto.getAsString(key).length();
            if (min > length || max <= length)
                throw new IllegalArgumentException(String.format("参数%s长度应该在%d~%d之间", key, min, max));
        }
    }

}
